const {
  getWechat,
  getOAuth
} = require("../../wechat");
const util = require("../../wechat-lib/util")
exports.getSignature = async (url) => {
  const client = getWechat()
  const data = await client.fetchAccessToken()
  const token = data.access_token
  const ticketData = await client.fetchTicket(token)
  const ticket = ticketData.ticket
  let params = util.sign(ticket, url)
  params.appId = client.appID

  return params
}

exports.getAuthorizeURL = (target, state, scope) => {
  const oauth = getOAuth()
  const url = oauth.getAuthorizeURL(target, state, scope)
  return url
}
exports.getUserinfoByCode = async (code, openID) => {
  const oauth = getOAuth()
  const data = await oauth.fetchAuthorizeAccessToken(code, openID)
  const userData = await oauth.getUserInfo(data.access_token, data.openid)
  return userData
}
